/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.lib;

import org.jdom.Element;
import ru.ssau.entities.Message;
import ru.ssau.entities.Thread;
import ru.ssau.entities.User;
import ru.ssau.utils.XMLHelper;

/**
 *
 * @author Дима
 */
public class Response implements java.io.Serializable, XMLizable {

    public static final int USER = 4;
    public static final int MESSAGE = 2;
    public static final int THREAD = 1;
    public static final int DELETE = 1;
    public static final int ADD = 2;
    public static final int REPLACE = 4;
    public static final int STATE_NOTIFICATION = 8;
    public static final int SET = 16;
    public static final int GET = 32;
    public static final int CHANGE = 64;
    private int entityType;
    private int action;
    private StringIntegerPair[] messages;
    private StringIntegerPair[] users;
    private StringIntegerPair[] threads;
    private StringIntegerPair messageListElement;
    private StringIntegerPair threadListElement;
    private StringIntegerPair userListElement;
    private Message message;
    private User user;
    private ru.ssau.entities.Thread thread;
    private Boolean canChangeModel;

    public Element toXMLElement() {
        Element root = new Element("Response");
        XMLHelper xhelp = new XMLHelper();
        xhelp.addElement(root, "entityType", entityType);
        xhelp.addElement(root, "action", action);
        if (users != null) {
            xhelp.addElement(root, "users", (XMLizable[]) users);
        }
        if (messages != null) {
            xhelp.addElement(root, "messages", (XMLizable[]) messages);
        }
        if (threads != null) {
            xhelp.addElement(root, "threads", (XMLizable[]) threads);
        }
        if (messageListElement != null) {
            xhelp.addElement(root, "messageListElement", messageListElement);
        }
        if (threadListElement != null) {
            xhelp.addElement(root, "threadListElement", threadListElement);
        }
        if (userListElement != null) {
            xhelp.addElement(root, "userListElement", userListElement);
        }
        if (message != null) {
            xhelp.addElement(root, "message", message);
        }
        if (thread != null) {
            xhelp.addElement(root, "thread", thread);
        }
        if (user != null) {
            xhelp.addElement(root, "user", user);
        }
        if (canChangeModel != null) {
            xhelp.addElement(root, "canChangeModel", canChangeModel);
        }
        return root;
    }

    public void loadFromXMLElement(Element data) {
        XMLHelper xhelp = new XMLHelper();
        Element temp = null;
        if ((temp = data.getChild("entityType")) != null) {
            setEntityType(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("action")) != null) {
            setAction(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("users")) != null) {
            setUsers(xhelp.loadStringIntegerPairs(temp));
        }
        if ((temp = data.getChild("messages")) != null) {
            setMessages(xhelp.loadStringIntegerPairs(temp));
        }
        if ((temp = data.getChild("threads")) != null) {
            setThreads(xhelp.loadStringIntegerPairs(temp));
        }
        StringIntegerPair buf = new StringIntegerPair();
        if ((temp = data.getChild("messageListElement")) != null) {
            buf.loadFromXMLElement(temp.getChild("StringIntegerPair"));
        }
        setMessageListElement(buf);
        if ((temp = data.getChild("threadListElement")) != null) {
            buf.loadFromXMLElement(temp.getChild("StringIntegerPair"));
        }
        setThreadListElement(buf);
        if ((temp = data.getChild("userListElement")) != null) {
            buf.loadFromXMLElement(temp.getChild("StringIntegerPair"));
        }
        setUserListElement(buf);
        Message nMessage = new Message();
        if ((temp = data.getChild("message")) != null) {
            nMessage.loadFromXMLElement(temp.getChild("Message"));
        }
        setMessage(nMessage);
        Thread nThread = new Thread();
        if ((temp = data.getChild("thread")) != null) {
            nThread.loadFromXMLElement(temp.getChild("Thread"));
        }
        setThread(nThread);
        User nUser = new User();
        if ((temp = data.getChild("user")) != null) {
            nUser.loadFromXMLElement(temp.getChild("User"));
        }
        setUser(nUser);
        if ((temp = data.getChild("canChangeModel")) != null) {
            setCanChangeModel(Boolean.parseBoolean(temp.getValue()));
        }
    }

    public Response() {
    }

    public StringIntegerPair getMessageListElement() {
        return messageListElement;
    }

    public void setMessageListElement(StringIntegerPair messageListElement) {
        this.messageListElement = messageListElement;
    }

    public StringIntegerPair getThreadListElement() {
        return threadListElement;
    }

    public void setThreadListElement(StringIntegerPair threadListElement) {
        this.threadListElement = threadListElement;
    }

    public StringIntegerPair getUserListElement() {
        return userListElement;
    }

    public void setUserListElement(StringIntegerPair userListElement) {
        this.userListElement = userListElement;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public int getEntityType() {
        return entityType;
    }

    public void setEntityType(int entityType) {
        this.entityType = entityType;
    }

    public StringIntegerPair[] getMessages() {
        return messages;
    }

    public void setMessages(StringIntegerPair[] messages) {
        this.messages = messages;
    }

    public StringIntegerPair[] getThreads() {
        return threads;
    }

    public void setThreads(StringIntegerPair[] threads) {
        this.threads = threads;
    }

    public StringIntegerPair[] getUsers() {
        return users;
    }

    public void setUsers(StringIntegerPair[] users) {
        this.users = users;
    }

    public Boolean getCanChangeModel() {
        return canChangeModel;
    }

    public void setCanChangeModel(Boolean canChangeModel) {
        this.canChangeModel = canChangeModel;
    }
}
